#include <logbase.h>
#include <genrand.h>
#include <pthread.h>
#include <sys/msg.h>
#include <cstring.h>
#include <threadExcuter.h>
#include <lxMessege.h>

#define MES_CHECK_KEY_ID 12345
#define MES_CHECK_RCV_ID_A 11111
#define MES_CHECK_RCV_ID_B 11112
#define MES_CHECK_RCV_ID_C 11113
void *MsgCheckThread(void *ptr)
{
    struct msgDtStct tmpDataRcv;
    struct msgOptStct tmpMsgC;
    struct thrdExeStct *tmpthrd = (struct thrdExeStct *)ptr;
    msgOptInit(&tmpMsgC, MES_CHECK_KEY_ID, MES_CHECK_RCV_ID_C);
    logdebugc("c thread begin");
    while (tmpthrd->threadRunflag)
    {
        usleep(5000);
        if (msgOptRcv(&tmpMsgC, &tmpDataRcv) < 0)
        {
            continue;
        }
        logdebugc("c rcv[rmt=%ld,snd=%ld,size=%d]data=%s", tmpDataRcv.rmtId, tmpDataRcv.sndId, tmpDataRcv.msgSize, tmpDataRcv.msgData);
        msgOptSendStr(&tmpMsgC, "c send to back", tmpDataRcv.sndId, 0);
        // logdebugc("c snd[rmt=%ld,snd=%ld,size=%d]data=%s", tmpDataSend.rmtId, tmpDataSend.sndId, tmpDataSend.msgSize, tmpDataSend.msgData);
    }
    msgOptQuit(&tmpMsgC);
    return NULL;
}

int main()
{
    struct msgDtStct tmpDataRcv;
    struct msgOptStct tmpMsgA;
    struct msgOptStct tmpMsgB;
    msgOptInit(&tmpMsgA, MES_CHECK_KEY_ID, MES_CHECK_RCV_ID_A);
    msgOptInit(&tmpMsgB, MES_CHECK_KEY_ID, MES_CHECK_RCV_ID_B);
    msgOptClean(&tmpMsgA, 1);
    struct thrdExeStct tmpthrd;
    thrdExeInit(&tmpthrd, MsgCheckThread, NULL);
    thrdExeBegin(&tmpthrd);
    sleep(1);
    for (int i = 0; i < 6; i++)
    {
        if (i == 0)
        {
            msgOptSendStr(&tmpMsgA, "a send to a", tmpMsgA.myId, 0);
        }
        else if (i == 1)
        {
            msgOptSendStr(&tmpMsgA, "a send to b", tmpMsgB.myId, 0);
        }
        else if (i == 2)
        {
            msgOptSendStr(&tmpMsgB, "b send to a", tmpMsgA.myId, 0);
        }
        else if (i == 3)
        {
            msgOptSendStr(&tmpMsgB, "b send to b", tmpMsgB.myId, 0);
        }
        else if (i == 4)
        {
            msgOptSendStr(&tmpMsgA, "a send to c", MES_CHECK_RCV_ID_C, 0);
        }
        else if (i == 5)
        {
            msgOptSendStr(&tmpMsgB, "b send to c", MES_CHECK_RCV_ID_C, 0);
        }
        // logdebugc("main snd[rmt=%ld,snd=%ld,size=%d]data=%s", tmpDataSend.rmtId, tmpDataSend.sndId, tmpDataSend.msgSize, tmpDataSend.msgData);
        // usleep(10000);
        usleep(100000);
        if (msgOptRcv(&tmpMsgA, &tmpDataRcv) < 0)
        {
            logdebugc("a no data");
        }
        else
        {
            logdebugc("a rcv[rmt=%ld,snd=%ld,size=%d]data=%s", tmpDataRcv.rmtId, tmpDataRcv.sndId, tmpDataRcv.msgSize, tmpDataRcv.msgData);
        }
        if (msgOptRcv(&tmpMsgB, &tmpDataRcv) < 0)
        {
            logdebugc("b no data");
        }
        else
        {
            logdebugc("b rcv[rmt=%ld,snd=%ld,size=%d]data=%s", tmpDataRcv.rmtId, tmpDataRcv.sndId, tmpDataRcv.msgSize, tmpDataRcv.msgData);
        }
    }
    thrdExeQuit(&tmpthrd);
    msgOptQuit(&tmpMsgA);
    msgOptQuit(&tmpMsgB);
    return -1;
}
